package com.mark.basic.controller;

import com.mark.basic.dto.LoginDto;
import com.mark.basic.util.AjaxResult;
import com.mark.basic.util.BusinessException;
import com.mark.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILogininfoService logininfoService;
    @PostMapping("/account")
    public AjaxResult login(@RequestBody LoginDto loginDto){
        try {
            Map<String, Object> map = logininfoService.loginAccount(loginDto);
            // 将map中的数据传入AjaxResult中的resultObj
            return AjaxResult.me().setResultObj(map);

        } catch (BusinessException e) {// 自定义业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMassage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMassage("系统繁忙");
        }
    }
    @PostMapping("/wechat")
    public AjaxResult weChat(@RequestBody Map<String,String> params){
        try {
            // 所以业务交给service
            return logininfoService.loginWeChat(params);
        } catch (BusinessException e) {// 自定义业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMassage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMassage("系统繁忙");
        }
    }
}